home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MPW_TOOL / TOOLS / TOOLS_WI / ICON_8 / ICONT_FO / TRANS.C < prev    next >
Text File  |  1990-03-02  |  4KB  |  169 lines

  1. /*
  2.  * trans.c - main control of the translation process.
  3.  */
  4.  
  5. #include "::h:config.h"
  6. #include "tproto.h"
  7. #include "::h:version.h"
  8. #include "globals.h"
  9. #include "trans.h"
  10. #include "general.h"
  11. #include "tsym.h"
  12. #include "tree.h"
  13. #include "token.h"
  14.  
  15. /*
  16.  * Prototypes.
  17.  */
  18.  
  19. FILE    *preprocess    Params((char *filename));
  20. novalue    trans1        Params((char *filename));
  21.  
  22. char *comfile = NULL;
  23.  
  24. int tfatals;            /* total number of fatal errors */
  25. int nocode;            /* non-zero to suppress code generation */
  26. int in_line;            /* current input line number */
  27. int incol;            /* current input column number */
  28. int peekc;            /* one-character look ahead */
  29.  
  30. FILE *srcfile;            /* current input file */
  31. FILE *codefile;            /* current ucode output file */
  32. FILE *globfile;            /* current global table output file */
  33.  
  34. /*
  35.  * translate a number of files, returning an error count
  36.  */
  37. int trans(ifiles)
  38. char **ifiles;
  39.    {
  40.    tmalloc();            /* allocate memory for translation */
  41.  
  42. #ifdef MultipleRuns
  43.    yylexinit();            /* initialize lexical analyser */
  44.    tcodeinit();            /* initialize code generator */
  45. #endif                    /* Multiple Runs */
  46.  
  47.    while (*ifiles)
  48.       trans1(*ifiles++);    /* translate each file in turn */
  49.    tmfree();            /* free memory used for translation */
  50.  
  51.    /*
  52.     * Report information about errors and warnings and be correct about it.
  53.     */
  54.    if (tfatals == 1)
  55.       fprintf(stderr, "1 error\n");
  56.    else if (tfatals > 1)
  57.       fprintf(stderr, "%d errors\n", tfatals);
  58.    else if (!silent)
  59.       fprintf(stderr, "No errors\n");
  60.  
  61.    return tfatals;
  62.    }
  63.  
  64. /*
  65.  * translate one file.
  66.  */
  67. static novalue trans1(filename)
  68. char *filename;
  69. {
  70.    char oname[MaxFileName];    /* buffer for constructing file names */
  71.  
  72.    comfile = filename;
  73.    tfatals = 0;    /* reset error counts */
  74.    nocode = 0;            /* allow code generation/*
  75.    in_line = 1;            /* start with line 1, column 0 */
  76.    incol = 0;
  77.    peekc = 0;            /* clear character lookahead */
  78.  
  79.    if (m4pre)
  80.       srcfile = preprocess(filename);
  81.    else if (strcmp(filename,"-") == 0) {
  82.       srcfile = stdin;
  83.       filename = "stdin";
  84.       }
  85.    else
  86.       srcfile = fopen(filename,"r");
  87.    if (srcfile == NULL)
  88.       quitf("cannot open %s",filename);
  89.    if (!silent)
  90.       fprintf(stderr, "%s:\n",filename);
  91.  
  92. #ifndef VarTran
  93.    /*
  94.     * Form names for the .u1 and .u2 files and open them.
  95.     *  Write the ucode version number to the .u2 file.
  96.     */
  97.  
  98.    makename(oname, TargetDir, filename, U1Suffix);
  99.    codefile = fopen(oname, "w");
  100.    if (codefile == NULL)
  101.       quitf("cannot create %s", oname);
  102.  
  103.    makename(oname, TargetDir, filename, U2Suffix);
  104.    globfile = fopen(oname, "w");
  105.    if (globfile == NULL)
  106.       quitf("cannot create %s", oname);
  107.    writecheck(fprintf(globfile,"version\t%s\n",UVersion));
  108. #endif                    /* VarTran */
  109.  
  110.    tok_loc.n_file = filename;
  111.    in_line = 1;
  112.  
  113.    tminit();                /* Initialize data structures */
  114.    yyparse();                /* Parse the input */
  115.  
  116.    /*
  117.     * Close the output files and the input file.
  118.     */
  119.  
  120. #ifndef VarTran
  121.    if (fclose(codefile) != 0 || fclose(globfile) != 0)
  122.       quit("cannot close ucode file");
  123. #endif                    /* VarTran */
  124.  
  125.    if (!m4pre) 
  126.       fclose(srcfile);
  127.    /* "else" is below in conditional */
  128.  
  129. #if UNIX
  130.    else if (pclose(srcfile) != 0)
  131.       quit("m4 terminated abnormally");
  132. #endif                    /* UNIX */
  133.    }
  134.  
  135. /*
  136.  * writecheck - check the return code from a stdio output operation
  137.  */
  138. novalue writecheck(rc)
  139.    {
  140.    if (rc < 0)
  141.       quit("cannot write to ucode file");
  142.    }
  143.  
  144. /*
  145.  * open a pipe to the preprocessor.
  146.  */
  147. static FILE *preprocess(filename)
  148. char *filename;
  149. {
  150.  
  151. #if MACINTOSH
  152. #if MPW
  153. /* #pragma unused(filename) */
  154.    return NULL;            /* to prevent compiler warning */
  155. #endif                    /* MPW */
  156. #endif                    /* MACINTOSH */
  157.  
  158. #if UNIX
  159.       {
  160.       FILE *f, *popen();
  161.       char *s = alloc((unsigned int)(4+strlen(filename)));
  162.       sprintf(s,"m4 %s",filename);
  163.       f = popen(s,"r");
  164.       free(s);
  165.       return f;
  166.       }
  167. #endif                    /* UNIX */
  168. }
  169.